package com.niguang.work.api.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.toolkit.CollectionUtils;
import com.niguang.common.constant.ResultCode;
import com.niguang.common.pojo.Result;
import com.niguang.common.util.ApiTokenUtils;
import com.niguang.work.api.entity.ReformUser;
import com.niguang.work.api.entity.WechatUser;
import com.niguang.work.api.mapper.ReformUserMapper;
import com.niguang.work.api.mapper.WechatUserMapper;
import com.niguang.work.api.service.ReformUserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 'work.v_personSite' is not BASE TABLE 服务实现类
 * </p>
 *
 * @author 12380
 * @since 2018-09-06
 */
@Service
public class ReformUserServiceImpl extends ServiceImpl<ReformUserMapper, ReformUser> implements ReformUserService {

    @Resource
    private WechatUserMapper wechatUserMapper;

    @Resource
    private ApiTokenUtils apiTokenUtils;

    @Override
    public Result getAddressBook(String type, Long siteId, String key, Page page) {
        List<Map<String, Object>> addressBook = baseMapper.getAddressBook(type, siteId, key, page);
        Map<String, Object> map = new HashMap<>(4);
        page.setRecords(addressBook);
        map.put("total", page.getTotal());
        map.put("list", page.getRecords());
        map.put("pages", page.getPages());
        map.put("current", page.getCurrent());
        return Result.build(ResultCode.SUCCESS, "通信录查询成功~", map);
    }

    @Override
    public Result getSiteList(String keyText) {
        List<Map<String, Object>> siteList = wechatUserMapper.getSiteList(apiTokenUtils.getUnionId(),keyText);
        return Result.build(ResultCode.SUCCESS, "员工工地列表获取成功~", siteList);
    }

    @Override
    public Result checkUser() {
        WechatUser wechatUser = wechatUserMapper.checkUser(apiTokenUtils.getUnionId());
        return Result.build(ResultCode.SUCCESS, "用户信息查询成功~", wechatUser);
    }

}
